<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Session;
use app\common\controller\Login;
use think\Request;
class Cash extends Login
{
    public function index()
    {
        $num = session::get('admin');

        $user = db('user')->where('id',$num)->find();

        $agent = $user['agent_id'];

        $data = Db::table('business_cash_get')->where('agent_id',$agent)->alias('c')
            ->Field('c.id as cid,i.*,c.*')
            ->join('business_info i','c.business_id = i.business_id','left')
            ->order('cash_status','asc')->paginate(10);

        $this->assign('data',$data);

        return $this->fetch();
    }

    public function allowed($id)
    {
        Db::table('business_cash_get')->where('id',$id)->update(['cash_status'=>1]);

        return $this->redirect('index');
    }

    public function unallowed($id)
    {
        Db::table('business_cash_get')->where('id',$id)->update(['cash_status'=>2]);

        $num = Db::table('business_cash_get')->where('id',$id)->find();

        $count = $num['cash_money'];

        $model =  Db::table('business_cash')->where('business_id',$num['business_id'])->find();

        $price = $count+$model['cash_num'];

        Db::table('business_cash')->where('business_id',$num['business_id'])->update(['cash_num'=>$price]);

        return $this->redirect('index');
    }
}